package br.org.ifrr.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.org.ifrr.model.Empenho;
import br.org.ifrr.repository.Empenhos;
import br.org.ifrr.util.cdi.CDIServiceLocator;

@FacesConverter(forClass = Empenho.class)
public class EmpenhoConverter implements Converter {

	// @Inject
	private Empenhos empenhos;

	public EmpenhoConverter() {
		empenhos = CDIServiceLocator.getBean(Empenhos.class);
	}

	@Override
	public Object getAsObject(FacesContext context, UIComponent componente,
			String value) {
		Empenho retorno = null;

		if (value != null) {
			Long id = new Long(value);
			retorno = empenhos.porId(id);
		}
		return retorno;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {

		if (value != null) {
			Empenho empenho = (Empenho) value;
			return empenho.getId() == null ? null : empenho.getId().toString();
		}
		return "";
	}
}
